// pages/popup/popup.js
import {
    getSearchWindowData,
    clearnHistoryData,
    searchCurrent,
    getgoodsList as PortgetgoodsList
} from "../../request/request.js"
let timer = null;
Page({
    data: {
        isShow: 1,
        keyword: '',
        historyKeywordList: [],
        hotKeywordList: [],
        keyWordList: [], //关键词列表
        goodsList: [], //商品列表
        filterCategory: [],
        order: '', //排序类型,
        categoryId: '' //商品类型

    },
    onCancel() {
        wx.switchTab({
            url: '/pages/index/index',
        })
    },
    //分类商品
    orderCategroy(e) {
        // console.log(e.detail);
        this.setData({
            categoryId: e.detail
        })
        this.getGoodsList()

    },
    //价格排序函数
    orderPrice(e) {
        // console.log( e.detail);
        this.setData({
            order: e.detail
        })
        this.getGoodsList()
    },
    //清除历史记录函数
    clearnHistory() {
        clearnHistoryData().then(res => {
            if (res.errno != 0) return
            this.setData({
                historyKeywordList: []
            })
        }).catch()
    },
    //获取商品列表
    getGoodsList() {
        let params = {
            keyword: this.data.keyword,
            page: 1,
            size: 20,
            // order: this.data.order,
            categoryId: this.data.categoryId,
            // sort: "price"

        }
        if (this.data.order !== '') {
            params['order'] = this.data.order
            params['sort'] = "price"
        }

        PortgetgoodsList(params).then(res => {
            if (res.errno != 0) return
            this.setData({
                goodsList: res.data.goodsList,
                filterCategory: res.data.filterCategory
            })
        })
    },
    //点击标签填充到搜索框
    getTagName(e) {
        //通过e事件对象，获取到子组件triggerEvent传过来的参数
        // console.log(e.detail.tagname);
        this.setData({
            keyword: e.detail,
            isShow: 3
        })
        //搜索商品列表
        this.getGoodsList()
        // PortgetgoodsList({
        //     keyword: this.data.keyword,
        //     page: 1,
        //     size: 20,
        //     order: this.data.order,
        //     categoryId: "0",
        //     sort: "price"

        // }).then(res => {
        //     if (res.errno != 0) return
        //     this.setData({
        //         goodsList: res.data.goodsList,
        //         filterCategory: res.data.filterCategory
        //     })
        // })
    },
    //聚焦搜索显示关键词列表
    showKeywordList() {
        this.setData({
            isShow: 2,

        })
        searchCurrent(this.data.keyword).then(res => {
            if (res.errno != 0) return
            this.setData({
                keyWordList: res.data
            })
        }).catch()
    },
    //实时搜索
    realtimeSearchFunction(e) {
        //当输入框没有数据时，显示历史记录和热门搜索
        if (e.detail.length <= 0) {
            this.setData({
                isShow: 1
            })
            clearTimeout(timer)
            return
        }
        //防抖函数
        if (timer) clearTimeout(timer)
        timer = setTimeout(() => {
            this.setData({
                keyword: e.detail
            })
            //调用上面的函数执行搜索
            this.showKeywordList()
        }, 500)
    },
    onLoad: function () {
        getSearchWindowData().then(res => {
            if (res.errno != 0) return
            const {
                defaultKeyword,
                historyKeywordList,
                hotKeywordList
            } = res.data

            this.setData({
                keyword: defaultKeyword.keyword,
                historyKeywordList: historyKeywordList,
                hotKeywordList: hotKeywordList
            })
        }).catch(err => {

        })
    }
})